<script src="http://maps.google.com/maps?file=api&amp;v=2" type="text/javascript"></script>
	<script type="text/javascript">

		function Marker(item)
		{	
			this.onGotImages = function(response)
			{
				var expected = '{\"ResultSet\":{';
				if (response.substring(0, expected.length) == expected)
				{
					var obj = eval( '(' + response + ')' );
					var results = obj.ResultSet.Result;
					mapGadget.thumbsLoaded = results.length;

					var dimension = Math.floor(Math.sqrt(mapGadget.thumbCount));
					var imgSize = Math.floor( (mapGadget.paneWidth - 1* dimension)/ dimension) - 2;
					var table = '<span></span><table style="width:' + mapGadget.paneWidth  + 'px;" cellspacing ="1" cellpadding="0">';
					for (var i=0; i< results.length; i+=dimension)
					{
						table += mapGadget.getRow(results, i, i + dimension, imgSize);
					}
					table += '</table>';

					var thumbTable = _gel("thumbs" + mapGadget.id);
					if (thumbTable)
					{
						thumbTable.innerHTML = table;					
					}
				}
			};
		}

		var mapGadget = 
		{
			prefs : new Object(),

			source :  "",
			stored_source :  "",

			map : new GMap2(_gel("map")),
			items : new Array(),

			paneWidth : 200,

			marker : new Object(),
			markerHTML : "",
			id : -1,

			cached : true,
			imageArray : new Array(),
			loadedImages : 0,

			thumbsLoaded : 0,
			thmubTable : "",
			thumbCount : 16,

			imageFeed : "http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=AyfZsPDV34E4NtYkfxBPYIz7GaiSP2rgJSZJMwRhUNnAT0vrPi.P08OJbXB1HzfgMg--",
			isIG : false,

			getUserPrefs : function()
			{
				mapGadget.source = mapGadget.prefs.getString("visitor_url");
				mapGadget.stored_source = mapGadget.prefs.getString("stored_visitor_url");

				var thumbCount = mapGadget.prefs.getInt("thumbGrid");
				mapGadget.thumbCount = thumbCount * thumbCount;

				mapGadget.paneWidth = mapGadget.prefs.getInt("thumbGridWidth");

				if ((mapGadget.source == null) || (mapGadget.source == ""))
				{	
					mapGadget.isIG = true;
					_IG_AdjustIFrameHeight();
				}
			},

			getGVisitObject : function(expr)
			{
				if (!expr)return null;
				var expected = "if(typeof GVisit != 'object') GVisit = {};";
				if (expr.substring(0, expected.length) == expected)
				{
					var GVisit = new Object();
					eval(expr.substring(expected.length + 1, expr.length));
					return GVisit;
				}
				return null;
			},

			getVisitorData : function()
			{
				var url = mapGadget.source;
			
				//if not on a webpage
				if (mapGadget.isIG)
				{
					url = mapGadget.stored_source

					//store last location - otherwise permission denied : Window.setProperty
					GEvent.addListener(mapGadget.map, "moveend", function() 
					{
						var center = mapGadget.map.getCenter();
						mapGadget.prefs.set("map_location", center.toUrlValue());
						mapGadget.prefs.set("map_zoom", mapGadget.map.getZoom());
					});
				}

				_IG_FetchContent(url, mapGadget.onGotVisitorData);
			},

			onGotVisitorData : function(response)
			{
				var data = mapGadget.getGVisitObject(response);

				if (data != null)
				{
					//store response object
					mapGadget.data = data;
					
					//for each visitor
					for (var i=0; i<data.visitors.length; i++)
					{
						var item = data.visitors[i];
						mapGadget.items[i] = data.visitors[i];
						if (item)
						{
							var locationString = item.name + (item.region != item.name ?', ' + item.region :'');
						
							var url = mapGadget.imageFeed + "&results=" + mapGadget.thumbCount;
							url += "&output=json";
							url += "&query=" + encodeURIComponent(locationString);
							
							var date = new Date();
							date.setTime(item.time * 1000);

							mapGadget.markerHTML = mapGadget.getMarkerHTML(i, locationString, date);
							
							var point = new GLatLng(item.lat, item.lon);
							mapGadget.marker = mapGadget.createMarker(point, i, mapGadget.markerHTML);						
							mapGadget.map.addOverlay(mapGadget.marker);
						}
					}
				}
			},


			renderMap : function()
			{
				var mapZoom = mapGadget.prefs.getInt("map_zoom");
				var mapLocation = mapGadget.prefs.getString("map_location");
				
				if (mapLocation == "")
				{
					mapLocation = new GLatLng(0,0);
				}
				else
				{
					coords = mapLocation.split(",");
					mapLocation = new GLatLng(_trim(coords[0]), _trim(coords[1]));
				}

				mapGadget.map.setCenter(mapLocation, mapZoom, G_SATELLITE_MAP);
				mapGadget.map.addControl(new GSmallMapControl());
				mapGadget.map.addControl(new GMapTypeControl());

			},

			addPoint : function()
			{

			},

			createMarker : function(point, i, text)
			{
				mapGadget.marker = text;
				var marker = new GMarker(point);

					// Show this marker's index in the info window when it is clicked
					try
					{
						GEvent.addListener(marker, "click", function() 
						{ 
							//if on iGoogle
							var container 
							//if (mapGadget.isIG)
							if (false)
							{
								var gadgetDiv = _gel("locationDiv");
								gadgetDiv.style.display = "";
								gadgetDiv.innerHTML = text;
								container = _gel("miniMap");

								var close = new Image();
								close.src = _IG_GetImageUrl("http://www.google.com/intl/en_ALL/mapfiles/close.gif");
								close.style.position = "absolute";
								close.style.right = "2px";
								close.style.top = "2px";
								close.style.cursor = "pointer";

								close.onclick = function()
								{
									gadgetDiv.style.display = "none";
									gadgetDiv.innerHTML = "";
								};
								gadgetDiv.appendChild(close);
							}
							else
							{
								marker.openInfoWindowHtml(text);
								container = _gel("miniMap" + i);
							}

							
							container = _gel("miniMap" + i);
							var minimap = new GMap(container);
							minimap.setCenter(point, 16);

							if (__UP_miniMapControls__ > 0)
							{
								minimap.addControl(new GSmallMapControl());
							}
						
							if (__UP_miniMapControls__ > 1)
							{
								minimap.addControl(new GMapTypeControl());
							}

							
							minimap.setMapType(G_SATELLITE_MAP);

							if (mapGadget.thumbCount > 0)
							{						
								mapGadget.loadedImages = 0;
								mapGadget.getThumbTable(mapGadget.items, i);
							}

						});
					}
					catch (err)
					{
					}

				return marker;
			},

			addImageSearch : function()
			{
				GEvent.addListener(mapGadget.map, "moveend", function()
				{
				  var center = mapGadget.map.getCenter();
				});

			},

			getMarkerHTML : function (i, locationString, date)
			{
				var dateString = date.toUTCString();

				//var tableWidth = mapGadget.isIG ? 'width:100%;' : '';

				var pw = mapGadget.isIG ? '100%' : (mapGadget.paneWidth - 2) + 'px';
				var infoWindowWidth =  mapGadget.paneWidth;

				var thumbCell = '&nbsp';
				if (mapGadget.thumbCount > 0)
				{
					thumbCell = '<div class="wraptocenter"><span></span><div style="text-align:center; width: '+mapGadget.paneWidth+'px; height: '+mapGadget.paneWidth+'px;" id="thumbs' + i +'">Loading images...</div></div>';
					infoWindowWidth = infoWindowWidth * 2;
				}
				else
				{
					infoWindowWidth = 200;
				}

				var miniMapWidth = (mapGadget.thumbCount > 0) ?  mapGadget.paneWidth : 200;


				var marker =	'<div id="infowindow" style="background-color:white; width:' +  infoWindowWidth + 'px;">';
				marker +=			'<div style="text-align:left; font-size:80%; padding-right:10px; width:' + infoWindowWidth +'px;">';
				marker +=				'Somebody in <b>' + locationString +'</b> visited';
				marker +=				'<br/>';
				marker +=				'on <b>' + dateString + '</b>';
				marker +=			'</div>';
				marker +=			'<div style="text-align:center;">';
				marker +=				'<table cellpadding="0" cellspacing="1" style="width:' + infoWindowWidth +'px;">';
				marker +=				'<tr>';
				marker +=					'<td valign="middle">';
				marker +=						'<div style="width:' + mapGadget.paneWidth + 'px; text-align:center; overflow:hidden; height: '+(mapGadget.paneWidth)+'px;" id="miniMap' + i + '">&nbsp;</div>';
				marker +=					'</td>';
				marker +=					'<td valign="middle">';
				marker +=						thumbCell;
				marker +=					'</td>';
				marker +=				'</tr>';
				marker +=				'</table>';
				marker +=			'</div>';
				marker +=		'</div>';
	
				return marker;
			},

			getImageThumb : function(results, i, size)
			{
				var result = results[i];

				if (__UP_showImage__)
				{
					mapGadget.imageArray[i] = new Image();
					mapGadget.imageArray[i].src = (mapGadget.cached ? _IG_GetImageUrl(result.Url) : result.Url);
				}

				var thumb =	'<a class="thumbnail" target="_top" href="' + result.Url +'">' +
							'<span></span><img onload="mapGadget.checkLoaded()"' +
							'class="thumb" src="' + (mapGadget.thumbCount > 1 ? result.Thumbnail.Url : result.Url) + '" width="' + size + '" height="' + size + '"'+
							'title="' + result.Summary + '"/>';
				if (__UP_showImage__)
				{
					thumb += '<span>';
					thumb += mapGadget.thumbCount > 1 ? '<img src="' + mapGadget.imageArray[i].src +'"/>' : '';
					thumb += '<span>';
				}
				thumb += '</a>';

				return thumb;
			},

			checkLoaded : function(e)
			{
				 mapGadget.loadedImages++;
				 if (mapGadget.loadedImages == mapGadget.thumbsLoaded)
				 {
					//console.log("done");
				 }
			},

			getCell : function(results, i, size)
			{
					var result = results[i];
				
					var cell = '<td style="display: table-cell; vertical-align: middle; width:'+size+'px; height:'+size+'px;">' + 
									'<div class="wraptocenter" style="height:'+size+'">' + 
										(result ? mapGadget.getImageThumb(results, i, size) : '&nbsp') + 
								'	</div>' + 
								'</td>';
					return cell;
			},

			getRow : function(results, start, limit, imgSize)
			{
				var row = '<tr>';
				for (var i=start; i<limit; i++)
				{
					row += mapGadget.getCell(results, i, imgSize);
				}
				row += '</tr>';
				return row;
			},
				
			getThumbTable : function (locationResults, i)
			{
				var locationResult = locationResults[i];
				var locationString = locationResult.name + (locationResult.region != locationResult.name ?', ' + locationResult.region :'');

				var url = mapGadget.imageFeed + "&results=" + mapGadget.thumbCount;
				url += "&output=json";
				url += "&query=" + encodeURIComponent(locationString);
				
				mapGadget.id = i

				_IG_FetchContent(url, new Marker(locationResult).onGotImages);


			},

			main : function()
			{
				if (GBrowserIsCompatible())
				{
					mapGadget.prefs = new _IG_Prefs(__MODULE_ID__)
					mapGadget.renderMap();
					mapGadget.getUserPrefs();

					_gel("map").style.height = mapGadget.prefs.getInt("gadgetHeight");
					
					//mapGadget.addImageSearch();
					mapGadget.getVisitorData();
				}
				_IG_Analytics("UA-582167-13", "/");
			}
		};

	  _IG_RegisterOnloadHandler(mapGadget.main());
  
    </script>